package iedcyt.acute.dataAccess.dao.acute1;



import iedcyt.acute.dataAccess.domain.acute1.Author_Sign;
import iedcyt.acute.dataAccess.generic.SpringDao;
import iedcyt.acute.dataAccess.idao.acute1.IAuthor_SignDao;

import java.util.ArrayList;
import java.util.List;


/**
 * Acceso a los datos de la tabla Author Sign
 * @author 53385255R
 *
 */
public class Author_SignDao extends SpringDao implements IAuthor_SignDao  {

	/**
	 * @see iedcyt.acute.dataAccess.idao.acute1.IAuthor_SignDao#create()
	 */
	@Override
	public void create() {
		//comprobamos si la tabla existe y si no la creamos
		try {
			_jdbcTemplate.queryForInt("select count(*) from Author_Sign");
		} catch (Exception e) {
			_jdbcTemplate.execute("CREATE TABLE Author_Sign (idAu numeric, idSign numeric)");
			_jdbcTemplate.execute("CREATE INDEX idxAuthor_Sign on Author_Sign (idAu, idSign) WITH PRIMARY DISALLOW NULL");
			_jdbcTemplate.execute("ALTER TABLE Author_Sign ADD FOREIGN KEY (idAu) REFERENCES Author(idAu)");
			//TODO: Comento esto que salta una excepcion 
			//_jdbcTemplate.execute("ALTER TABLE Author_Sign ADD FOREIGN KEY (idSign) REFERENCES Sign(idSign)");
			
		}
	}
	
	@Override
	public List<Author_Sign> selectAll() {
		
		
		return _jdbcTemplate.query("SELECT * FROM Author_Sign",
				new Author_SignRowMapper());
	}
	
	@Override
	public void deleteAll() {
		
		
		_jdbcTemplate.update("DELETE from Author_Sign");
	}

	

	@Override
	public void insert(Author_Sign doc) {
	
		_jdbcTemplate.update("INSERT INTO Author_Sign (idAu, idSign) VALUES (?,?)",
				new Object[] { doc.getIdAu(), doc.getIdsign() });
		
	}
	
	
	@Override
	public List<Author_Sign> selectByIdAuthor(int[] idsAuthor) {
		
		if (idsAuthor.length != 0)
		{
		String where = "idAu = ? ";
		int cnt = 1;
		for (int i = 0; i < idsAuthor.length -1 ; i++)
		{
			cnt++;
			where += "OR idAu = ? ";
		}
		
		Object[] objs = new Object[cnt];
		
		for (int i = 0; i < idsAuthor.length ; i++)
		{
			objs[i] = idsAuthor[i];
		}
		
		return _jdbcTemplate
				.query(
						"SELECT * FROM Author_Sign WHERE " + where,
						objs,
						new Author_SignRowMapper());
		}
		else return new ArrayList<Author_Sign>();
	}

	@Override
	public List<Author_Sign> selectByIdSign(int[] idsSign) {
		
		if (idsSign.length != 0)
		{
		String where = "idSign = ? ";
		int cnt = 1;
		for (int i = 0; i < idsSign.length -1 ; i++)
		{
			cnt++;
			where += "OR idSign = ? ";
		}
		
		Object[] objs = new Object[cnt];
		
		for (int i = 0; i < idsSign.length ; i++)
		{
			objs[i] = idsSign[i];
		}
		
		
		return _jdbcTemplate
				.query(
						"SELECT * FROM Author_Sign WHERE " + where,
						objs,
						new Author_SignRowMapper());
		}
		else return new ArrayList<Author_Sign>();
	}
	
	
	

	@Override
	public void delete(int idAu, int idSign) {
		_jdbcTemplate.update("DELETE from Author_Sign WHERE idAu=? AND idSign=?",
				new Object[] { idAu, idSign  });
		
	}

	@Override
	public void deleteByIdAu(int idAu) {
		_jdbcTemplate.update("DELETE from Author_Sign WHERE idAu=?",
				new Object[] { idAu  });
		
	}

	@Override
	public List<Author_Sign> selectByIdAuByIdSign(int idAu, int idSign) {
		return _jdbcTemplate
		.query(
				"SELECT * FROM Author_Sign WHERE idAu = ? AND idSign = ?",
				new Object[] { idAu, idSign  },
				new Author_SignRowMapper());
	}

}

